<?php
require_once APPPATH.'third_party/base_controller'.EXT;
    class Server extends Base_Controller
    {
        function __construct()   
        {
            parent::__construct();
        }
            
        function index()
        {
            $data = array();
           
            $data['setting'] = $setting = $this->setting_model->get_setting();
			$data['title'] =  "Liên hệ";
            $this->load_render($this->load->view('server',$data,TRUE));
        }
        function send_email()
        {
            $setting = $this->setting_model->get_setting();
            $name = $this->input->get('name');
            $address = $this->input->get('address');
            $phone = $this->input->get('phone');
            $email = $this->input->get('email');
            $comment = $this->input->get('comment');
            $capcha_code = $this->input->get('capcha_code');
            $smtp_secure = "ssl";
            $smtp_server = "smtp.gmail.com";
            $smtp_port = $setting->smtp_port;
            $smtp_username = $setting->smtp_user;
            $smtp_password = $setting->smtp_pass;
            
            $html = "";
            $html .= "Họ và tên: <b>$name</b><br>";
            $html .= "Email: <b>$email</b><br>";
            $html .= "Phone: <b>$phone</b><br>";
            $html .= "Địa chỉ: <b>$address</b><br>";
            $html .= "Comment: <b>$comment</b><br>";
            
            include_once $_SERVER['DOCUMENT_ROOT'] . '/vnmod/public/securimage/securimage.php';
			$securimage = new Securimage();
            if ($securimage->check($capcha_code) == false) {
                $error = "Capcha nhập vào không đúng";
            } else {                
                   
                    $config['email_recevie'] = $setting->email_company;
                    $config['sitename'] = $setting->title_tv;
                    include_once $_SERVER['DOCUMENT_ROOT'] . '/vnmod/public/class_email/class.phpmailer.php';
					
                    $mail = new PHPMailer(true);
					$mail->IsSMTP();					
					$mail->SMTPDebug  = 1;
					$mail->SMTPAuth   = true;
					$mail->SMTPSecure = $smtp_secure;
					$mail->Host       = $smtp_server;
					$mail->Port       = $smtp_port;
					$mail->Username   = $smtp_username;
					$mail->Password   = $smtp_password;
					$mail->CharSet = 'UTF-8';;
					$mail->AddReplyTo($email, $name);
					$mail->AddAddress($config['email_recevie'], $config['sitename']);
					$mail->SetFrom($email, $name);
					$mail->Subject = 'Liên hệ từ '.$config['sitename'];
					$mail->AltBody = 'Liên hệ từ '.$config['sitename'];
					$mail->MsgHTML($html);			
                    if(!$mail->Send()) {
                      $error = "Mailer Error: " . $mail->ErrorInfo;
                    } else {
                      $error =  "Chúng tôi sẽ phản hồi nhanh nhất. Chân thành cảm ơn quý khách.";
                    }
                    $process=1;
            }
           
            
            $data = array(
                'status' => $error
            );
            echo json_encode($data);
        }
    }
?>    